package edu.nf.project.services.employeeAndCustomerInformation.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import edu.nf.project.entitys.entity.employeeAndCustomerInformation.convention.CustomerInfo;
import edu.nf.project.mappers.employeeAndCustomerInformation.convention.CustomerInfoMapper;
import edu.nf.project.mappers.employeeAndCustomerInformation.relevancy.CustomerInfo_CouponMapper;
import edu.nf.project.services.employeeAndCustomerInformation.CustomerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author gjh
 * @Date 2025/6/3
 * @Description:
 */
@Service
public class CustomerInfoServiceImpl implements CustomerInfoService {
    @Autowired
    private CustomerInfoMapper customerInfoMapper;

    @Autowired
    CustomerInfo_CouponMapper infoCouponMapper;

    /**
     * 展示用户积分
     * @param t_id u_id
     * @return
     */
    @Override
    public CustomerInfo findCustomerInfoPoint(String t_id) {
        LambdaQueryWrapper<CustomerInfo> wrapper = new LambdaQueryWrapper<CustomerInfo>();
        wrapper.eq(CustomerInfo::getUId,t_id);

        CustomerInfo customerInfo = customerInfoMapper.selectOne(wrapper);
        if (customerInfo != null) {
            return customerInfo; // 正确返回积分
        }
        return null;
    }

    @Override
    public void deleteCoupon(Integer couponId) {
        infoCouponMapper.deleteById(couponId);
    }

    /**
     * 更新用户积分
     * @param customerId u_id
     * @param point 积分
     * @return
     */
    @Override
    public Boolean updateCouponCustomer(Integer customerId, Integer point) {
        LambdaUpdateWrapper<CustomerInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CustomerInfo::getUId, customerId)
                .set(CustomerInfo::getPoint, point);

        int rows = customerInfoMapper.update(null, wrapper);
        return rows > 0;
    }

    @Override
    public CustomerInfo getCustomerByUserId(Integer uId) {
        LambdaQueryWrapper<CustomerInfo> queryWrapper = new LambdaQueryWrapper<CustomerInfo>();
        queryWrapper.eq(CustomerInfo::getUId,uId);

        return customerInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public Boolean updSurplus(Integer uId, Integer surplus) {
        LambdaUpdateWrapper<CustomerInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CustomerInfo::getUId, uId)
                .set(CustomerInfo::getSurplus, surplus);
        return customerInfoMapper.update(null, wrapper) > 0;
    }

    @Override
    public Integer useCusIdFindUId(Integer uId) {
        LambdaUpdateWrapper<CustomerInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CustomerInfo::getUId, uId);
        return customerInfoMapper.selectOne(wrapper).getCustomerId();
    }
}
